package com.school.dao.impl;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.school.dao.StudentDao;
import com.school.domain.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private DataSource dataSource = new ComboPooledDataSource();
    private QueryRunner queryRunner = new QueryRunner(dataSource);
    @Override
    public boolean add(Student student) {
        int result;
        try {
            result = queryRunner.update("insert into student values(?, ?, ?, ?, ?, ?, ?)", null, student.getName(), student.getPwd(), student.getAge(), student.getSex(), student.getClid(), null);

        } catch (SQLException throwables) {
            return false;
        }
        return result == 1;
    }

    @Override
    public boolean del(long sid) {
        int result;
        try {
            result = queryRunner.update("delete from student where sid = ?", sid);
        } catch (SQLException throwables) {
            return false;
        }
        return result == 1;
    }

    @Override
    public boolean update(Student student) {
        int result;
        Long rid = student.getRid();
        // 如果值是零表示还没占座
        if (rid == 0) rid = null;
        try {
            result = queryRunner.update("update student set name = ?, pwd = ?, age = ?, sex = ?, clid = ?, rid = ? where sid = ?", student.getName(), student.getPwd(), student.getAge(), student.getSex(), student.getClid(), rid, student.getSid());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
        return result == 1;
    }

    @Override
    public Student findById(long sid) {
        List<Student> execute;
        try {
            execute = queryRunner.query("select * from student where sid = " + sid, new BeanListHandler<>(Student.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
        if (execute.size() < 1) return null;
        return execute.get(0);
    }

    @Override
    public List<Student> findByName(String name) {
        List<Student> execute;
        try {
            execute = queryRunner.query("select * from student where name = " + name, new BeanListHandler<>(Student.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
        return execute;
    }

    @Override
    public List<Student> findAll() {
        List<Student> execute;
        try {
            execute = queryRunner.query("select * from student", new BeanListHandler<>(Student.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
        return execute;
    }

    @Override
    public List<Student> findByClid(long clid) {
        List<Student> execute;
        try {
            execute = queryRunner.query("select * from student where clid = " + clid, new BeanListHandler<>(Student.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
        return execute;
    }


}
